define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#rtTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.project/index',
        add_url: 'mall.project/add',
        edit_url: 'mall.project/edit',
        delete_url: 'mall.project/delete',
        export_url: 'mall.project/export',
        modify_url: 'mall.project/modify',
        tree: 'mall.project/tree',
    };


    var init2 = {
        table_elem: '#archivesFile',
        table_render_id: 'currentTableRenderId1',
        index_url: 'mall.project/archivesFile',
        borrow_url: 'mall.project/archivesBorrow',
        show_url: 'mall.project/archivesShow',
        xml_url: 'mall.project/archivesXml',
    };
    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                url: ea.url(init.index_url),
                page: true, //开启分页
                limit: 15,
                toolbar: [],
                // search:true,
                defaultToolbar: [],

                cols: [[ //表头
                    {type: 'checkbox'}
                    , {field: 'keys', title: '序号', sort: true, width: 70}
                    , {field: 'reference', title: '档号', sort: true, width: 150}
                    , {
                        field: 'name',
                        title: '案卷名称',
                        sort: true,
                        align: 'left',
                        minWidth: 1000,
                        templet: function (value, row, index) {
                            console.log(value, row, index)
                            // value = JSON.stringify(value)
                            // 假设每一行数据有一个 id 字段，你想根据这个 id 生成链接
                            var url = '/mall.project/archivesFile.html?id=' + value.id; // 根据 id 拼接链接地址

                            return '<a style="color:#00b1ec" href="' + url + '" target="_self">' + value.name + '</a>'; // 返回带有链接的 HTML 字符串
                        }
                    }
                    , {field: 'page_number', title: '页次', sort: true, width: 80}
                    , {field: 'start_end_date', title: '起止日期', sort: true, width: 180}
                    , {field: 'term', title: '保存期限', selectList: {1: '永久', 2: '20年', 3: '30年'}, sort: true, width: 100}
                    , {width: 250, title: '操作', templet: ea.table.tool},
                ]],
            });

            ea.listen();
            var tree = layui.tree;
            var table = layui.table;
            var project_id = $("#project_id").val()
            ea.request.get({
                    url: 'tree?project_id=' + project_id,
                },
                function (res) {
                    res.data = res.data || [];
                    tree.render({
                        elem: '#node_ids',
                        data: res.data,
                        showCheckbox: false,
                        id: 'currentTableId'
                        , isJump: true //是否允许点击节点时弹出新窗口跳转
                        , click: function (obj) {
                            var data = obj.data;  //获取当前点击的节点数据
                            //执行搜索重载
                            var tableId = $(this).attr('data-table-reset');
                            console.log(data)
                            if (tableId === undefined || tableId === '' || tableId == null) {
                                tableId = init.table_render_id;
                            }
                            if (data.status == 0) {
                                table.reload('currentTableRenderId', {
                                    page: {
                                        curr: 1
                                    }
                                    , where: {
                                        reference: data.number,
                                        op: '{}'
                                    }
                                }, 'data');
                            }

                        }
                    });


                }
            )

            $('.layui-btn').on('click', function () {
                table.reload('currentTableRenderId', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        term: $("#term").val(),
                        status: $("#status").val(),
                        keyword: $("#keywords").val(),
                        relation: $("#relation").val(),
                        duty: $("#duty").val(),
                        status_stype: $("#status_stype").val(),
                        times: $("#test10").val(),
                    }
                }, 'data');
            });
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        archivesFile: function () {

            var id = $('#archives_id').val()
            var reference = $('#reference').val()


            ea.table.render({
                init: init2,
                url: ea.url(init2.index_url) + '?id=' + id,
                page: true, //开启分页
                limit: 15,
                cols: [[ //表头
                    {type: 'checkbox'},
                    {field: 'keys', title: '序号', sort: true, width: 90},
                    {field: 'duty', title: '责任者', sort: true, width: 190},
                    {field: 'name', title: '文件题名', sort: true},
                    {field: 'time', title: '日期', sort: true, width: 150},
                    {field: 'start_page_number', title: '页次', sort: true, width: 120},
                    {
                        field: 'group_name',
                        title: '原件形式',
                        sort: true,
                        width: 120,
                        templet: function (value, row, index) {
                            console.log(value, row, index)
                            var html = '<span style="color: #f59410;">扫描文件</span>';
                            if (value.is_shop !== "") {
                                html = '<span style="color: #e651f5;">电子文件</span>';
                            }
                            return html
                        }
                    },
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                text: '借阅',
                                url: init2.borrow_url + '?id=' + id + '&type=2&reference=' + reference,
                                method: 'open',
                                auth: 'archivesBorrow',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                                extend: 'data-full="false"',
                            }, {
                                text: '查看',
                                url: init2.show_url + '?id=' + id + '&type=2',
                                method: 'open',
                                auth: 'archivesShow',
                                class: 'layui-btn layui-btn-xs layui-btn-danger',
                            }, {
                                text: 'xml',
                                url: init2.xml_url + '?id=' + id + '&type=2',
                                method: 'open',
                                auth: 'archivesXml',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                            }],
                        ]
                    }
                ]],
                toolbar: [],
                defaultToolbar: ['filter'],
            });
            ea.listen();
            $('#searchBtn').on('click', function () {
                layui.table.reload('currentTableRenderId1', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        keyword: $("#keywords").val(),
                    }
                }, 'data');
            });
        },
        archivesBorrow: function () {


            ea.listen();
        },
        archivesShow: function () {
            ea.listen();
        },
        archivesXml: function () {
            ea.listen();
        }
    };
    return Controller;
});